# encoding:UTF-8
# 爬取图片 
import re
import urllib   
import threading
import time
import Queue

def getHtml(url):
	return urllib.urlopen(url).read()


def getUrl(html):
	pattern = r'http://.*?\.jpg!mid'
	imare=re.compile(pattern)
	imglist=re.findall(imare,html)
	return imglist
def getImgList(url):
	return getUrl(getHtml(url))

def getSourceUrl(html):
	pattern=r'http://girl-atlas.com/a/\d+'
	imare=re.compile(pattern)
	imglist=re.findall(imare,html)
	return imglist

# http://girl-atlas.com/a/10150202015800000173
class ImgDown(threading.Thread):
	def __init__(self,queue):
		threading.Thread.__init__(self)
		self.queue=queue
		self.start()

	def run(self):
		global count 
		while (True):
			imgurl = self.queue.get()
			
			imglist2=getSourceUrl(imgurl)  

			for i in range(len(imglist2)):
				imglist3 = getImgList(imglist2[i])
				for j in range(len(imglist3)):
					self.queue.append(imglist3[j])

			print "线程%s: down %s"%(self.getName(),imgurl)
			urllib.urlretrieve(imgurl,"/pic/down/%s.jpg"%count)
			count+=1
			if self.queue.empty():
				break
			self.queue.task_done()


def main():
	global count 
	url="http://girl-atlas.com/a/10150202015800000173"
	imglist=getImgList(url)
	threads=[]
	count=0
	queue=Queue.Queue();

	for i in range(len(imglist)):
		queue.put(imglist[i])

	for i in range(4):
		thread=ImgDown(queue)
		threads.append(thread)

	#queue.join()
	#合并进程，当子进程结束时，主进程才可以执行
	for thread in threads:
		thread.join()
 
    #另一种保持主进程阻塞的方法，次方法和前面的self.queue.task_tone()相照应
    #两个要同时使用
    #queue.join()

if __name__=='__main__':
    main()
    print "Down"
